package com.thinkfit.jy.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.common.lib.bean.VideoSectionBean;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;

import java.util.ArrayList;

/**
 * 圆形进度条控件
 * Author: JueYes jueyes_1024@163.com
 * Time: 2019-08-07 15:38
 */

public class RunWayView extends View {

    private Paint mBackPaint, mProgressPaint;   // 绘制画笔
    private int mCurrent, mDuration;      // 圆环进度(0-100)
    private ArrayList<VideoSectionBean> mVideoSections;

    private RectF[] mBackRectF, mProgressRectF;

    private Bitmap[][] mBitmaps;

    private int[] mColors = {Color.parseColor("#15CFB3"), Color.parseColor("#E6D88B"), Color.parseColor("#F6606D")};

    public RunWayView(Context context) {
        this(context, null);
    }

    public RunWayView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        mBitmaps = new Bitmap[3][2];
        mBitmaps[0][0] = BitmapFactory.decodeResource(getResources(), R.drawable.app_water_off);
        mBitmaps[0][1] = BitmapFactory.decodeResource(getResources(), R.drawable.app_water_on);
        mBitmaps[1][0] = BitmapFactory.decodeResource(getResources(), R.drawable.app_data_off);
        mBitmaps[1][1] = BitmapFactory.decodeResource(getResources(), R.drawable.app_data_on);
        mBitmaps[2][0] = BitmapFactory.decodeResource(getResources(), R.drawable.app_fire_off);
        mBitmaps[2][1] = BitmapFactory.decodeResource(getResources(), R.drawable.app_fire_on);
    }

    public RunWayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 初始化背景圆环画笔
        mBackPaint = new Paint();
        mBackPaint.setStyle(Paint.Style.FILL);    // 只描边，不填充
        mBackPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        mBackPaint.setAntiAlias(true);              // 设置抗锯齿
        mBackPaint.setDither(true);                 // 设置抖动

        // 初始化进度圆环画笔
        mProgressPaint = new Paint();
        mProgressPaint.setStyle(Paint.Style.FILL);    // 只描边，不填充
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        mProgressPaint.setAntiAlias(true);              // 设置抗锯齿
        mProgressPaint.setDither(true);                 // 设置抖动
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mVideoSections == null) {
            return;
        }
        int size = mVideoSections.size();
        if (mBackRectF == null) {
            mBackRectF = new RectF[size];
            mProgressRectF = new RectF[size];
            int padding = BaseUtils.StaticParams.dp2px(getContext(), 6);

            int totalWidth = getWidth() - padding * (mVideoSections.size() - 1);
            int height = BaseUtils.StaticParams.dp2px(getContext(), 4);
            float startX = 0;
            float startY = BaseUtils.StaticParams.dp2px(getContext(), 18);
            for (int i = 0; i < size; ++i) {
                float fWidth = (mVideoSections.get(i).getEndtime() - mVideoSections.get(i).getStartime()) * totalWidth * 1.0f / mDuration;
                mBackRectF[i] = new RectF(startX, startY, startX + fWidth, startY + height);
                mProgressRectF[i] = new RectF(startX, startY, startX, startY + height);
                if (i < size - 1) {
                    startX += fWidth + padding;
                }
            }
        }
        int round = BaseUtils.StaticParams.dp2px(getContext(), 4);
        for (int i = 0; i < size; ++i) {
            try {
                if (mCurrent > mVideoSections.get(i).getStartime()) {
                    canvas.drawBitmap(mBitmaps[mVideoSections.get(i).getLevel()][1], mBackRectF[i].left, 0f, mBackPaint);
                    mProgressPaint.setColor(mColors[mVideoSections.get(i).getLevel()]);
                    if (mCurrent >= mVideoSections.get(i).getEndtime()) {
                        canvas.drawRoundRect(mBackRectF[i], round, round, mProgressPaint);
                    } else {
                        canvas.drawRoundRect(mBackRectF[i], round, round, mBackPaint);
                        float w = (mCurrent - mVideoSections.get(i).getStartime()) * mBackRectF[i].width() / (mVideoSections.get(i).getEndtime() - mVideoSections.get(i).getStartime());
                        mProgressRectF[i].right = mBackRectF[i].left + w;
                        canvas.drawRoundRect(mProgressRectF[i], round, round, mProgressPaint);
                    }
                } else {
                    canvas.drawBitmap(mBitmaps[mVideoSections.get(i).getLevel()][0], mBackRectF[i].left, 0f, mBackPaint);
                    canvas.drawRoundRect(mBackRectF[i], round, round, mBackPaint);
                }
            } catch (Exception e) {
            }
        }
    }

    public void initData(int duration, ArrayList<VideoSectionBean> list) {
        mDuration = duration;
        mVideoSections = list;
        invalidate();
    }

    public void setProgress(int current) {
        mCurrent = current;
        postInvalidate();
    }
}
